import {} from 'react'
import style from './index.module.less'
import { useAppDispatch, useAppSelector } from '@src/redux/hook'
import { List, Dialog, Toast } from 'antd-mobile'
import { UploadOutline, EditSOutline, FileOutline, MessageOutline } from 'antd-mobile-icons'
import { push } from '@src/redux/reducers/pages'
import { logout } from '@src/redux/reducers/basic'
import { awaitWrap } from '@src/assets/js/tool'
import { logoutRequest } from './ajax'
import { root } from '@src/assets/js/config'
// import uuid from 'uuid'

async function logoutHandle (callback) {
  Toast.show({
     icon: 'loading',
    content: '退出中',
    duration: 0
  })
  const [e] = await awaitWrap(logoutRequest())
  if (e === null) {
    Toast.clear()
    callback()
  }
}

const { Item } = List

function Main () {
  const userInfo = useAppSelector(state => state.basic.userInfo)
  const dispatch = useAppDispatch()
  return (
    <div className={style.container}>
      <div className={style.header}>
        <div className={style.name}>{userInfo && userInfo.realname}</div>
        <div className={style.mobile}>{userInfo && userInfo.mobile}</div>
      </div>
      <List>
        <Item prefix={<FileOutline className={style.icon} />} onClick={() => {
          dispatch(push({ element: 'caselibList', title: '案例库' }))
        }}>案例库</Item>
        <Item prefix={<FileOutline className={style.icon} />} onClick={() => {
          dispatch(push({ element: 'knowledgelibList', title: '知识库' }))
        }}>知识库</Item>
        <Item prefix={<FileOutline className={style.icon} />} onClick={() => {
          dispatch(push({ element: 'doclibList', title: '文书下载' }))
        }}>文书下载</Item>
        <Item prefix={<MessageOutline className={style.icon} />} onClick={() => {
          dispatch(push({ element: 'aiChat', title: 'DS智能聊天' }))
        }}>DS智能聊天</Item>
        <Item prefix={<EditSOutline className={style.icon} />} onClick={() => {
          dispatch(push({ element: 'modifyPassword', title: '修改密码' }))
        }}>修改密码</Item>
        <Item prefix={<UploadOutline className={style.out} />} onClick={() => {
          Dialog.confirm({
            title: '您确认要退出吗？',
            onConfirm: () => {
              logoutHandle(() => {
                dispatch(logout())
                location.href = `${location.origin}${root}/gate/login`
                // dispatch(setList([{ element: 'login', title: '登录', id: uuid() }]))
              })
            }
          })
        }}>退出</Item>
      </List>
    </div>
  )
}

export default Main
